﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

 @if(HttpContext.Current.User.Identity.IsAuthenticated)
    {
        <script type="text/javascript">
            var _IsAuthenticated = ('@HttpContext.Current.User.Identity.IsAuthenticated' == 'True') ? true : false;
            var _UserName = '@HttpContext.Current.User.Identity.Name';
            var _FirstName = '';
            var _LastName = '';
        </script>
    }
    else
    {
        <script type="text/javascript">
            
        </script>
    }

<script type="text/javascript">
    
    Ext.onReady(function () {

        

        Ext.define('treeModel', {
            extend: 'Ext.data.Model',
            fields: ['text', 'link']
        });

        var treeStore = Ext.create('Ext.data.TreeStore', {
            model: 'treeModel',
            root: {
                expanded: true,
                children: [{
                        text: 'Quản lý công việc',
                        leaf: false,
                        children: [{
                                text: 'Giao việc',
                                link: '/Task/AssignTask',
                                leaf: true
                            }, {
                                text: 'Danh sách công việc',
                                link: '/Task/GetAll',
                                leaf: true
                            }, {
                                text: 'Gửi báo cáo tuần',
                                link: '/Report',
                                leaf: true
                            }, {
                                text: 'Xem báo cáo tuần',
                                link: '/Report/ViewReport',
                                leaf: true
                            }]
                    }, {
                        text: 'Quản lý thiết bị',
                        leaf: false,
                        children: [{
                                text: 'Danh sách thiết bị',
                                link: '/Device',
                                leaf: true
                            }, {
                                text: 'Danh mục thiết bị',
                                link: '/DeviceCategory',
                                leaf: true
                                //}, {
                            //    text: 'Lịch sử sử dụng',
                            //    link: 'abc',
                            //    leaf: true
                            //}, {
                            //    text: 'Lịch sử bảo trì',
                            //    link: 'abc',
                            //    leaf: true
                            }]
                    }, {
                        text: 'Khác',
                        leaf: false,
                        children: [{
                                text: 'Danh sách nhân sự',
                                link: '/Employee',
                                leaf: true
                            }, {
                                text: 'Danh sách phòng ban',
                                link: '/Department',
                                leaf: true
                            }, {
                                text: 'Danh sách chức danh',
                                link: '/JobTitle',
                                leaf: true
                                //}, {
                            //    text: 'Tool - Upload file',
                            //    link: 'abc',
                            //    leaf: true
                            }]
                        //}, {
                    //    text: 'Quản trị',
                    //    leaf: false,
                    //    children: {
                    //        text: 'Quản lý tài khoản',
                    //        link: 'abc',
                    //        leaf: true
                    //    }
                    }]
            }
        });

        var westPanel = Ext.create('Ext.tree.Panel', {
            title: 'Application Menu',
            region: 'west',
            collapsible: true,
            width: 200,
            maxWidth: 250,
            minWidth: 150,
            split: true,
            margins: '0 0 0 0',
            store: treeStore,
            rootVisible: false,
            listeners: {
                itemclick: function (tree, record, item, index, e, option) {

                    if (!record.data.leaf) {
                        return;
                    }

                    var nodeText = record.data.text;
                    var tabBar = tabPanel.getTabBar();
                    var tabIndex;
                    for (var i = 0; i < tabBar.items.length; i++) {
                        if (tabBar.items.get(i).getText() === nodeText) {
                            tabIndex = i;
                        }
                    }

                    if (Ext.isEmpty(tabIndex)) {
                        tabPanel.add({
                            xtype: 'uxiframe',
                            title: record.data.text,
                            src: record.data.link
                        });

                        tabIndex = tabPanel.items.length - 1;
                    }
                    tabPanel.setActiveTab(tabIndex);
                }
            }
        });

        var tabPanel = Ext.create('Ext.tab.Panel', {
            region: 'center',
            margins: '0 5 0 0',
            plain: true,
            defaults: {
                bodyPadding: 10,
                closable: true
            },
            items: {
                title: 'Trang chủ',
                closable: false
            }
        });

        Ext.create('Ext.container.Viewport', {
            layout: 'border',
            items: [{
                    region: 'north',
                    height: 55,
                    xtype: 'panel',
                    bodyStyle: 'background:#ebeaea;',
                    layout: {
                        type: 'hbox',
                        pack: 'end',
                        align: 'middle'
                    },
                    items: [{
                        xtype: 'button',
                        margins: '0 15 0 0',
                        id: 'btnUserInfo',
                        //glyph: 72,
                        text: 'Xin chào: ' + _UserName,
                        scale: 'medium',
                        //width: 200,
                        icon: '../Content/icon/user2.png',
                        menu: [{
                            text: 'Thoát',
                            icon: '../Content/icon/quit.png',
                            listeners: {
                                click: function() {
                                    Ext.Ajax.request({
                                        url: '/api/ApiLogin/LogOff',
                                        method: 'POST',
                                        success: function (transport, option) {
                                            var responseData = Ext.decode(transport.responseText);
                                            if (responseData.success == true) {
                                                window.location = "/Login";
                                            } else {
                                                Ext.MessageBox.show({
                                                    title: 'Có lỗi xảy ra trong khi thoát',
                                                    msg: 'Xin thực hiện lại sau',
                                                    icon: Ext.MessageBox.ERROR,
                                                    buttons: Ext.Msg.OK
                                                });
                                            }
                                        },
                                        failure: function (transport, option) {
                                            var responseData = Ext.decode(transport.responseText);
                                            Ext.MessageBox.show({
                                                title: 'Có lỗi xảy ra trong khi thoát',
                                                msg: responseData.message,
                                                icon: Ext.MessageBox.ERROR,
                                                buttons: Ext.Msg.OK
                                            });
                                        }
                                    });
                                }
                            }
                        }]
                    }]
                }, {
                    region: 'south',
                    height: 10,
                    xtype: 'container'
                }, westPanel, tabPanel]
        });

    });
    
    if (_IsAuthenticated) {
        Ext.Ajax.request({
            url: '/api/ApiLogin/GetUserInfo',
            method: 'GET',
            success: function (transport, option) {
                var responseData = Ext.decode(transport.responseText);
                if (responseData.success == true) {
                    var employee = responseData.data.Employee;
                    if (employee) {
                        _FirstName = employee.FirstName;
                        _LastName = employee.LastName;

                        Ext.getCmp('btnUserInfo').setText('Xin chào: ' + _FirstName + ' ' + _LastName);
                    }
                }
            }
        });
    }

</script>
@RenderBody()